package com.tools;

import android.app.Activity;
import android.content.Context;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class LoginCandidate {
    //保存候选登录
    public static void saveUserName(Activity activity,String name){
        FileOutputStream fos = null;
        try{
            //在原有内容上追加
            //fos = openFileOutput("txt", Context.MODE_APPEND);
            //覆盖原有内容
            fos = activity.openFileOutput("txt", Context.MODE_APPEND);
            String text = name+",";
            fos.write(text.getBytes());
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }finally {
            try {
                if(fos!=null) {
                    fos.flush();
                    fos.close();
                }
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }

    //读取候选登录
    public static void getUserName(Activity activity, List<String> autoUserName){
        FileInputStream fis = null;
        try{
            fis = activity.openFileInput("txt");
            if(fis.available()==0){
                return;
            }else{
                byte[] con = new byte[fis.available()];
                while(fis.read(con)!=-1){
                }
                String xx = new String(con);
                String[] str = xx.split(",");
                for (int i=0; i<str.length; i++) {
                    if(!autoUserName.contains(str[i])) {
                        autoUserName.add(str[i]);
                    }
                }
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}
